/*
 * cudaUtils.h
 *
 *  Created on: Feb 20, 2010
 *      Author: Łukasz Spintzyk
 */

#ifndef CUDAUTILS_H_
#define CUDAUTILS_H_
#include "ph.h"
#include <set>
#include <cuda.h>
#include <iostream>
#include <cuda_runtime_api.h>

class CUDAException : public std::exception
{
public:
	CUDAException(const std::string& what):
		d_what(what)
	{

	}
	virtual ~CUDAException() throw(){};

	virtual const char* what() const throw()
		{
		return d_what.c_str();
		}

private:
	std::string d_what;

};

void CUDA_CHECK(cudaError_t err);
#ifndef NDEBUG
#define CUDA_CHECK_old(x) assert(x == cudaSuccess)
#else
#define CUDA_CHECK_old(x) x
#endif


#ifndef NDEBUG
#define CUAssert(condition) if (condition) /* nothing */; else return
#else
#define CUAssert(condition) (void)(0)
#endif

void printLastError();


void writeToLockedMemory(byte* mem, byte* out);

class CudaMemoryManager
{
public:
	CudaMemoryManager();
	~CudaMemoryManager();
	void operator()(byte* mem);
	byte* malloc(uint32_t size, bool pageLocked = false);
	void malloc(uint32_t size, byte** ptr);
	void free(byte* mem);
	void copy(byte* dst, byte* src, uint32_t size);

private:
	std::set<byte*> d_memory;
	std::set<byte*> d_hostMemory;
};

//typedef byte* CudaPtr;


#endif /* CUDAUTILS_H_ */
